<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="dialogVisible = true">增加停车计费规则</el-button>
      <el-button @click="exportToExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column label="序号" type="index" :index="indexMethod" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式" prop="chargeType">
          <template #default="scope">
            {{ formatChargeType(scope.row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="float:right">
      <el-pagination
        :page-size="params.pageSize"
        layout="total, prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
    <addRule :dialog-visible.sync="dialogVisible" />
  </div>
</template>

<script>
import { getCarrule } from '@/api/carrule'
import { utils, writeFileXLSX } from 'xlsx'
import addRule from './components/addRule.vue'
export default {
  name: 'Building',
  components: { addRule },
  data() {
    return {
      dialogVisible: false,
      ruleList: [],
      params: {
        page: 1,
        pageSize: 2
      },
      total: 0
    }
  },
  watch: {
    'addForm.chargeType'(val) {
      // 每次切换清空所有校验
      this.$refs.addForm.clearValidate(['turnPrice', 'durationTime', 'durationPrice', 'partitionFrameTime', 'partitionFramePrice', 'partitionIncreaseTime', 'partitionIncreasePrice'])
    }
  },
  mounted() {
    this.getCarRuleList()
  },
  methods: {
    async getCarRuleList() {
      const res = await getCarrule(this.params)
      this.ruleList = res.data.rows
      this.total = res.data.total
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getCarRuleList()
    },
    indexMethod(row) {
      return (this.params.page - 1) * this.params.pageSize + row + 1
    },
    formatChargeType(type) {
      const MAP = {
        'duration': '时长收费',
        'turn': '按次收费',
        'partition': '分段收费'
      }
      return MAP[type]
    },
    // 导出exsx文件
    async exportToExcel() {
      // 表头英文字段key
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      // 表头中文字段value
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      const res = await getCarrule(this.params)
      const list = res.data.rows.map(item => {
        const obj = {}
        tableHeaderKeys.forEach(key => {
          key === 'chargeType' ? obj[key] = this.formatChargeType(item[key]) : obj[key] = item[key]
        })
        return obj
      })
      const workbook = utils.book_new()
      const worksheet = utils.json_to_sheet(list)
      utils.book_append_sheet(workbook, worksheet, 'Rule')
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
</style>
